//
//  CMDBShareManager.swift
//  CMDB
//
//  Created by 王磊 on 17/4/10.
//  Copyright © 2017年 CMDB. All rights reserved.
//

import UIKit
private let AppKey: String = ""

private let SinaWeibo_AppKey: String = ""

private let SinaWeibo_AppSecret: String = ""

private let SinaWeibo_AppUrl: String = ""

private let WeiChat_AppKey: String = ""

private let WeiChat_AppSecret: String = ""

//private let QQ_AppKey: String = ""
//
//private let QQ_AppSecret: String = ""
class CMDBShareManager: NSObject {
    
    static let getInstance: CMDBShareManager = CMDBShareManager()
    
    private override init() {
        super.init()
        
        
    }
}
extension CMDBShareManager: CMDBShareManagerDelegate {}
// 注册share sdk
// 需要 白名单 urltypes
/*
 
 <key>LSApplicationQueriesSchemes</key>
	<array>
 <string>weixin</string>
 <string>wechat</string>
 <string>sinaweibo</string>
 <string>sinaweibohd</string>
 <string>sinaweibosso</string>
 <string>weibosdk</string>
 <string>weibosdk2.5</string>
 <string>mqqapi</string>
 <string>mqq</string>
 <string>mqqOpensdkSSoLogin</string>
 <string>mqqconnect</string>
 <string>mqqopensdkdataline</string>
 <string>mqqopensdkgrouptribeshare</string>
 <string>mqqopensdkfriend</string>
 <string>mqqopensdkapi</string>
 <string>mqqopensdkapiV2</string>
 <string>mqqopensdkapiV3</string>
 <string>mqzoneopensdk</string>
 <string>wtloginmqq</string>
 <string>wtloginmqq2</string>
 <string>mqqwpa</string>
 <string>mqzone</string>
 <string>mqzonev2</string>
 <string>mqzoneshare</string>
 <string>wtloginqzone</string>
 <string>mqzonewx</string>
 <string>mqzoneopensdkapiV2</string>
 <string>mqzoneopensdkapi19</string>
 <string>mqzoneopensdkapi</string>
 <string>mqzoneopensdk</string>
	</array>
 */
// 依赖库
/*
 libicucore.tdb
 libsqlite3.tdb
 libz.tdb
 libstdc++.tdb
 JavaScriptCore.framework
 ImageIO.frameword
 */

extension CMDBShareManager {
    
    func regShareSDK() {
        ShareSDK.registerApp(AppKey, activePlatforms: [
            SSDKPlatformType.TypeSinaWeibo.rawValue,
            SSDKPlatformType.TypeTencentWeibo.rawValue,
            SSDKPlatformType.TypeWechat.rawValue,
            SSDKPlatformType.TypeQQ.rawValue], onImport: { (platform: SSDKPlatformType) in
                switch platform
                {
                case SSDKPlatformType.TypeSinaWeibo:
                    ShareSDKConnector.connectWeibo(WeiboSDK.classForCoder())
                case SSDKPlatformType.TypeWechat:
                    ShareSDKConnector.connectWeChat(WXApi.classForCoder())
                    //                case SSDKPlatformType.TypeQQ:
                //                    ShareSDKConnector.connectQQ(QQApiInterface.classForCoder(), tencentOAuthClass: TencentOAuth.classForCoder())
                default:
                    break
                }
                
        }) { (platform: SSDKPlatformType, appInfo: NSMutableDictionary?) in
            
            
            switch platform
            {
            case SSDKPlatformType.TypeSinaWeibo:
                //设置新浪微博应用信息,其中authType设置为使用SSO＋Web形式授权
                
                appInfo?.SSDKSetupSinaWeiboByAppKey(SinaWeibo_AppKey, appSecret: SinaWeibo_AppSecret, redirectUri: SinaWeibo_AppUrl, authType: SSDKAuthTypeBoth)
                
            case SSDKPlatformType.TypeWechat:
                //设置微信应用信息
                appInfo?.SSDKSetupWeChatByAppId(WeiChat_AppKey, appSecret: WeiChat_AppSecret)
                
                
                //            case SSDKPlatformType.TypeQQ:
                //                //设置QQ应用信息
                //                appInfo?.SSDKSetupQQByAppId(QQ_AppKey, appKey: QQ_AppSecret, authType: SSDKAuthTypeWeb)
                
            default:
                break
            }
        }
    }
}
// 获取share bean
extension CMDBShareManager {
    
    func queryShare(shareParam: CMDBShareParam, handler: CMDBShareBeanHandler, error: CMDBHttpErrorHandler, failure: CMDBHttpFailureHandler) {
        
        CMDBHttpManager.getInstance.request(shareParam.URLString(), param: shareParam.keyValues, success: { (sysTime, data, page) in
            
            let share = CMDBShareBean.objectWithKeyValues(data as! NSDictionary) as! CMDBShareBean
            
            handler(shareBean: share)
            }, error: error, failure: failure)
        
    }
}
// 分享
extension CMDBShareManager {
    
    func share(platformType: SSDKPlatformType, sharedBean: CMDBShareBean, handler: CMDBShareStateHandler) {
        
        let params: NSMutableDictionary = NSMutableDictionary()
        
        params.SSDKSetupShareParamsByText(sharedBean.content, images: NSURL(string: sharedBean.icon) , url: NSURL(string: sharedBean.url), title: sharedBean.title, type: .Auto)
        
        ShareSDK.share(platformType, parameters: params) { (state, userData, contentEntity, error) in
            
            handler(state: state)
        }
    }
}